﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebDS;
using WebUS;
using WebDS.CDBNames;
using IP.Core.IPCommon;
using System.Data;
using IP.Core.IPData.DBNames;
using IP.Core.IPData;
using IP.Core.IPUserService;

public partial class ChucNang_Ban_giao_thiet_bi : System.Web.UI.Page
{
    #region bienToanCuc
    US_V_DM_BAN_GIAO_THIET_BI m_us_v_dm_ban_giao_thiet_bi = new US_V_DM_BAN_GIAO_THIET_BI();
    DS_V_DM_BAN_GIAO_THIET_BI m_ds_v_dm_ban_giao_thiet_bi = new DS_V_DM_BAN_GIAO_THIET_BI();

    US_GD_BAN_GIAO_TB m_us_gd_ban_giao_thiet_bi = new US_GD_BAN_GIAO_TB();
    DS_GD_BAN_GIAO_TB m_ds_gd_ban_giao_thiet_bi = new DS_GD_BAN_GIAO_TB();
    US_DM_THIET_BI m_us_dm_thiet_bi = new US_DM_THIET_BI();
    DS_DM_THIET_BI m_ds_dm_thiet_bi = new DS_DM_THIET_BI();
    US_CM_DM_TU_DIEN m_us_cm_dm_tu_dien_hang_san_xuat = new US_CM_DM_TU_DIEN();
    DS_CM_DM_TU_DIEN m_ds_cm_dm_tu_dien_hang_san_xuat = new DS_CM_DM_TU_DIEN();
    US_CM_DM_TU_DIEN m_us_cm_dm_tu_dien_xuat_xu = new US_CM_DM_TU_DIEN();
    DS_CM_DM_TU_DIEN m_ds_cm_dm_tu_dien_xuat_xu = new DS_CM_DM_TU_DIEN();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        load_data_2_grid();
        if (!IsPostBack)
        {
            load_data_2_cbo_ten_ben_a();
        }
    }
    //Load du lieu vao US bang cach lay so thu tu hang
    private void load_data_2_us_details(int ip_i_stt_row)
    {
        decimal id_ban_giao_thiet_bi = CIPConvert.ToDecimal(m_grv_dm_doi_tac.DataKeys[ip_i_stt_row].Value);
        m_us_gd_ban_giao_thiet_bi = new US_GD_BAN_GIAO_TB(id_ban_giao_thiet_bi);
        //m_us_dm_thiet_bi = new US_DM_THIET_BI(m_us_gd_ban_giao_thiet_bi.dcID_THIET_BI);
        //us_cm_dm_tu_dien_xuat_xu = new US_CM_DM_TU_DIEN(m_us_dm_thiet_bi.dcXUAT_XU);
        //Truyen gia tri ID vao hidden field
        hdf_id_ban_giao_thiet_bi.Value = m_grv_dm_doi_tac.DataKeys[ip_i_stt_row].Value.ToString();
    }
    //Day du lieu tu US len luoi du lieu
    private void us_object_2_form()
    {
        m_cbo_ten_dia_phuong_benh_vien.SelectedValue = m_us_gd_ban_giao_thiet_bi.dcID_KHACH_HANG.ToString();
        m_txt_ten_ben_a_1.Text = m_us_gd_ban_giao_thiet_bi.strDAI_DIEN_BEN_A_1;
        m_txt_chuc_vu_ben_a_1.Text = m_us_gd_ban_giao_thiet_bi.strCHUC_VU_DAI_DIEN_BEN_A_1;
        m_txt_ten_ben_a_2.Text = m_us_gd_ban_giao_thiet_bi.strDAI_DIEM_BEN_A_2;
        m_txt_chuc_vu_ben_a_2.Text = m_us_gd_ban_giao_thiet_bi.strCHUC_VU_DAI_DIEN_BEN_A_2;
        m_txt_ten_ben_b_1.Text = m_us_gd_ban_giao_thiet_bi.strDAI_DIEN_BEN_B_1;
        m_txt_chuc_vu_ben_b_1.Text = m_us_gd_ban_giao_thiet_bi.strCHUC_VU_DAI_DIEN_BEN_B_1;
        m_txt_ten_ben_b_2.Text = m_us_gd_ban_giao_thiet_bi.strDAI_DIEN_BEN_B_2;
        m_txt_chuc_vu_ben_b_2.Text = m_us_gd_ban_giao_thiet_bi.strCHUC_VU_DAI_DIEN_BEN_B_2;
        m_txt_ten_may.Text = m_us_dm_thiet_bi.strTEN_MAY;
        m_txt_sn.Text = m_us_gd_ban_giao_thiet_bi.strSN;
        m_txt_ngay_ban_giao.SelectedDate = m_us_gd_ban_giao_thiet_bi.datNGAY_BAN_GIAO;
        m_txt_phu_kien_di_kem.Text = m_us_gd_ban_giao_thiet_bi.strPHU_KIEN;
        m_txt_model.Text = m_us_dm_thiet_bi.strMODEL;
        //xuat xu, hang san xuat
        m_txt_hang_san_xuat.Text = m_us_cm_dm_tu_dien_hang_san_xuat.strTEN;
        m_txt_xuat_xu.Text = m_us_cm_dm_tu_dien_xuat_xu.strTEN;
    }
    
    //Day du lieu tu form len US object
    private void form_2_us_object()
    {        
        try
        {
            m_us_gd_ban_giao_thiet_bi.dcID_KHACH_HANG = CIPConvert.ToDecimal(m_cbo_ten_dia_phuong_benh_vien.SelectedValue);
            m_us_gd_ban_giao_thiet_bi.strDAI_DIEN_BEN_A_1 = m_txt_ten_ben_a_1.Text;
            m_us_gd_ban_giao_thiet_bi.strCHUC_VU_DAI_DIEN_BEN_A_1 = m_txt_chuc_vu_ben_a_1.Text;
            m_us_gd_ban_giao_thiet_bi.strDAI_DIEM_BEN_A_2 = m_txt_ten_ben_a_2.Text;
            m_us_gd_ban_giao_thiet_bi.strCHUC_VU_DAI_DIEN_BEN_A_2 = m_txt_chuc_vu_ben_a_2.Text;
            m_us_gd_ban_giao_thiet_bi.strDAI_DIEN_BEN_B_1 = m_txt_ten_ben_b_1.Text;
            m_us_gd_ban_giao_thiet_bi.strCHUC_VU_DAI_DIEN_BEN_B_1 = m_txt_chuc_vu_ben_b_1.Text;
            m_us_gd_ban_giao_thiet_bi.strDAI_DIEN_BEN_B_2 = m_txt_ten_ben_b_2.Text;
            m_us_gd_ban_giao_thiet_bi.strCHUC_VU_DAI_DIEN_BEN_B_2 = m_txt_chuc_vu_ben_b_2.Text;
            m_us_gd_ban_giao_thiet_bi.datNGAY_BAN_GIAO = m_txt_ngay_ban_giao.SelectedDate;
            m_us_gd_ban_giao_thiet_bi.strPHU_KIEN = m_txt_phu_kien_di_kem.Text;
            m_us_gd_ban_giao_thiet_bi.strSN = m_txt_sn.Text;
            US_DM_SN_THIET_BI v_us_dm_sn_thiet_bi = new US_DM_SN_THIET_BI();
            v_us_dm_sn_thiet_bi.get_us_by_sn(m_txt_sn.Text);
            m_us_gd_ban_giao_thiet_bi.dcID_THIET_BI = v_us_dm_sn_thiet_bi.dcID_THIET_BI;
        }
        catch (Exception)
        {
            
            m_lbl_check_SN.Text = "Sai thông tin Serial number hoặc điền thiếu dữ liệu";
        }       
    }
    //Load du lieu vao datagridview
    private void load_data_2_grid()
    {
        if (m_txt_sn_search.Text == "")
        {
            try
            {
                //Do du lieu vao DS
                m_us_v_dm_ban_giao_thiet_bi.FillDataset(m_ds_v_dm_ban_giao_thiet_bi);
                //Do du lieu tu DS vao gridview
                m_grv_dm_doi_tac.DataSource = m_ds_v_dm_ban_giao_thiet_bi.V_DM_BAN_GIAO_THIET_BI;
                m_grv_dm_doi_tac.DataBind();

            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(this, v_e);
            }
        }
        else
        {
            try
            {
                //Do du lieu vao DS
                m_us_v_dm_ban_giao_thiet_bi.filldataset_dm_ban_giao_thiet_bi_search(m_txt_sn_search.Text, m_ds_v_dm_ban_giao_thiet_bi);
                //Do du lieu tu DS vao gridview
                m_grv_dm_doi_tac.DataSource = m_ds_v_dm_ban_giao_thiet_bi.V_DM_BAN_GIAO_THIET_BI;
                m_grv_dm_doi_tac.DataBind();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(this, v_e);
            }
        }
        
    }


    private void load_data_2_cbo_ten_ben_a()
    {
        try
        {
            US_DM_DIA_PHUONG_BENH_VIEN v_us_ten_dia_phuong_benh_vien = new US_DM_DIA_PHUONG_BENH_VIEN();
            DS_DM_DIA_PHUONG_BENH_VIEN v_ds_ten_dia_phuong_benh_vien = new DS_DM_DIA_PHUONG_BENH_VIEN();
            v_us_ten_dia_phuong_benh_vien.FillDataset(v_ds_ten_dia_phuong_benh_vien);
            m_cbo_ten_dia_phuong_benh_vien.DataSource = v_ds_ten_dia_phuong_benh_vien.DM_DIA_PHUONG_BENH_VIEN;
            m_cbo_ten_dia_phuong_benh_vien.DataTextField = DM_DIA_PHUONG_BENH_VIEN.TEN_DIA_PHUONG;
            m_cbo_ten_dia_phuong_benh_vien.DataValueField = DM_DIA_PHUONG_BENH_VIEN.ID;
            m_cbo_ten_dia_phuong_benh_vien.DataBind();
        }
        catch (Exception v_e)
        {

            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }


    protected void m_grv_dm_doi_tac_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            load_data_2_us_details(e.RowIndex);
            us_object_2_form();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
        
    }
    protected void m_btn_chon_Click(object sender, EventArgs e)
    {
        try
        {
            US_DM_SN_THIET_BI v_us_dm_sn_thiet_bi = new US_DM_SN_THIET_BI();
            v_us_dm_sn_thiet_bi.get_us_by_sn(m_txt_sn.Text);
            US_DM_THIET_BI v_us_dm_thiet_bi = new US_DM_THIET_BI(v_us_dm_sn_thiet_bi.dcID);
            //US_CM_DM_TU_DIEN v_us_cm_dm_tu_dien_hang_san_xuat = new US_CM_DM_TU_DIEN(v_us_dm_thiet_bi.dcHANG_SAN_XUAT);
            //US_CM_DM_TU_DIEN v_us_cm_dm_tu_dien_xuat_xu = new US_CM_DM_TU_DIEN(v_us_dm_thiet_bi.dcXUAT_XU);

            //m_txt_ten_may.Text = v_us_dm_thiet_bi.strTEN_MAY;
            //m_txt_hang_san_xuat.Text = v_us_cm_dm_tu_dien_hang_san_xuat.strTEN;
            //m_txt_model.Text = v_us_dm_thiet_bi.strMODEL;
            //m_txt_xuat_xu.Text = v_us_cm_dm_tu_dien_xuat_xu.strTEN;
            //m_lbl_check_SN.Text = "";
        }
        catch (Exception)
        {

            m_lbl_check_SN.Text = "Sai thông tin Serial number";
        }     
        
        
    }
    protected void m_btn_xoa_trang_Click1(object sender, EventArgs e)
    {
        m_cbo_ten_dia_phuong_benh_vien.SelectedIndex = 0;
        m_txt_ten_ben_a_1.Text = "";
        m_txt_chuc_vu_ben_a_1.Text = "";
        m_txt_ten_ben_a_2.Text = "";
        m_txt_chuc_vu_ben_a_2.Text = "";
        m_txt_ten_ben_b_1.Text = "";
        m_txt_chuc_vu_ben_b_1.Text = "";
        m_txt_ten_ben_b_2.Text = "";
        m_txt_chuc_vu_ben_b_2.Text = "";
        m_txt_ten_may.Text = "";
        m_txt_hang_san_xuat.Text = "";
        m_txt_model.Text = "";
        m_txt_xuat_xu.Text = "";
        m_txt_sn.Text = "";
        m_txt_phu_kien_di_kem.Text = "";
    }
    protected void m_btn_luu_du_lieu_ban_giao_Click(object sender, EventArgs e)
    {
        try
        {
            //Thu thap du lieu
            form_2_us_object();
            //Insert vao co so du lieu
            m_us_gd_ban_giao_thiet_bi.Insert();
            //Load lai datagridview
            load_data_2_grid();
            m_lbl_them_moi.Text = "Lưu dữ liệu thành công";
            m_lbl_check_SN.Text = "";
        }
        catch (Exception)
        {
            m_lbl_them_moi.Text = "Lưu dữ liệu thất bại";
        }
    }

    protected void m_grv_dm_doi_tac_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            US_GD_BAN_GIAO_TB v_us_gd_ban_giao_thiet_bi = new US_GD_BAN_GIAO_TB();
            v_us_gd_ban_giao_thiet_bi = new US_GD_BAN_GIAO_TB(CIPConvert.ToDecimal(m_grv_dm_doi_tac.DataKeys[e.RowIndex].Value));
            v_us_gd_ban_giao_thiet_bi.Delete();
            load_data_2_grid();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_btn_tim_kiem_Click(object sender, EventArgs e)
    {
        load_data_2_grid();
    }
}